<style type="text/css">
    .layui-table-body{overflow-x:auto;}
    .layui-table-cell{font-size:12px;}
</style>
<div class="layui-form hisi-search">
    <form action="{:url()}" method="get" id="hisi-table-search">
        <div class="layui-form-item">
            <label class="layui-form-label">门店</label>
            <div class="layui-input-inline">
                <select name="shop_id" class="layui-input field-shop_id" lay-verify="required" lay-search="">
                  <option value="">直接选择或搜索选择</option>
                  {volist name="shops" id='vo'}
                  <option value="{$vo.id}">{$vo.name}</option>
                  {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">会员</label>
            <div class="layui-input-inline">
                <select name="user_id" class="layui-input field-user_id" lay-verify="required" lay-search="">
                  <option value="">直接选择或搜索选择</option>
                  {$users|raw}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">开始时间</label>
          <div class="layui-input-inline">
            <!-- <input type="text" class="layui-input field-start_end" id="test6" name="start_end" placeholder=" - "> -->
            <input type="text" class="layui-input field-start" id="start" name="start" placeholder="yyyy-MM-dd">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">结束时间</label>
          <div class="layui-input-inline">
            <input type="text" class="layui-input field-end" id="end" name="end" placeholder="yyyy-MM-dd">
          </div>
        </div>
        <div class="layui-form-item hisi-search-btn">

            <div class="layui-input-inline" style="width:100%">
                <button type="submit" class="layui-btn layui-btn-normal layui-icon layui-icon-search">执行</button>
                <button type="button" class="layui-btn layui-btn-primary layui-icon layui-icon-download-circle import">导入余额</button>
                <button type="button" class="layui-btn layui-btn-warm layui-icon layui-icon-download-circle export">导出</button>
                <!-- <button type="reset" class="layui-btn layui-btn-primary layui-icon layui-icon-delete">重置</button> -->
            </div>
        </div>
    </form>
</div>
<table id="dataTable"></table>
<script type="text/html" id="usernameTpl">
    <div style="line-height:18px;">
    <p class="ml10 fl"><strong class="mcolor">{{ d.username }} ({{ d.nick }})</strong><br>手机：{{ d.mobile }}<br>邮箱：{{ d.email }}</p></div>
</script>
<script type="text/html" id="statusTpl">
    <input type="checkbox" name="status" value="{{ d.status }}" lay-skin="switch" lay-filter="switchStatus" lay-text="正常|关闭" {{ d.status == 1 ? 'checked' : '' }} data-href="{:url('status')}?id={{ d.id }}">
</script>
<script type="text/html" title="操作按钮模板" id="buttonTpl">
    <a href="{:url('edit')}?id={{ d.id }}" class="layui-btn layui-btn-xs layui-btn-normal hisi-iframe-pop"  hisi-data="{width: '550px', height: '420px', title: '修改会员'}">修改</a><a href="{:url('del')}?id={{ d.id }}" class="layui-btn layui-btn-xs layui-btn-danger j-tr-del">删除</a>

</script>
{include file="system@block/layui" /}
<script id="toolbar" type="text/html">
    <a href="{:url('add')}" class="layui-btn layui-btn-normal layui-btn-sm hisi-iframe-pop" hisi-data="{width: '550px', height: '420px', title: '添加会员'}">&nbsp;添加</a>

    <a data-href="{:url('del')}" class="layui-btn layui-btn-danger j-page-btns confirm layui-btn-sm">&nbsp;删除</a>
</script>



<script type="text/javascript">
    layui.use(['table','laydate','upload','excel'], function() {

        var table = layui.table;
        var laydate = layui.laydate;
        var upload = layui.upload;
        


        //日期范围
    laydate.render({
      elem: '#start'
    });
    laydate.render({
      elem: '#end'
    });
    //指定允许上传的文件类型
     upload.render({
       elem: '.import'
       ,url: '{:url('import')}' //改成您自己的上传接口
       ,accept: 'file' //普通文件
       ,done: function(res){
         layer.msg('上传成功');
         console.log(res);
       }
     });
        table.render({
            elem: '#dataTable'
            ,url: '{:url()}' //数据接口
            ,page: true //开启分页
            ,skin: 'row'
            ,even: true
            ,limit: 20
            ,text: {
                none : '暂无相关数据'
            }
            // ,toolbar: '#toolbar'
            ,defaultToolbar: ['filter']
            ,cols: [[ //表头
                {type:'checkbox'}
                ,{field: 'user_name', title: '会员'}
                ,{field: 'shop_name', title: '门店'}
                ,{field: 'type_text', title: '类型'}
                ,{field: 'is_give', title: '账户',templet:function(d){
                    if(d.is_give == 2){
                        return '赠送账户';
                    }else{
                        return '会员卡账户';
                    }
                }}
                ,{field: 'price', title: '金额',templet:function(d){
                    if(d.type == 1){
                        return '+' + d.price;
                    }else{
                        return '-' + d.price;
                    }
                }}

                ,{field: 'sn', title: '订单号'}
                ,{field: 'createtime', title: '系统时间',sort:true}
                // ,{title: '操作', width: 150, templet: '#buttonTpl'}
            ]]
            ,done: function(){
                if (typeof(layui.global.addTableTool) !== 'undefined') {
                    layui.global.addTableTool();
                }
            }
        });


        var excel = layui.excel;
          $(document).on('click','.export',function(){
              var form = $('#hisi-table-search').serializeArray();
              var where = new Array();
              form.push({name:'operation',value:2});
              $.ajax({
                 url: '{:url()}',
                 data:form,
                 dataType: 'json',
                 success: function(res) {
                     // 假如返回的 res.data 是需要导出的列表数据
                     console.log(res.data);// [{name: 'wang', age: 18, sex: '男'}, {name: 'layui', age: 3, sex: '女'}]
                     // 1. 数组头部新增表头
                     // 'ID','项目名称','数量','金额','手工','结算方式','职位','提成比例','添加时间','订单编号','会员','门店','员工','提成金额'
                    for (let i = 0; i < res.data.length; i++) {
                        res.data[i]['is_give'] = res.data[i]['is_give'] == 2 ? '赠送账户' : '会员卡账户';
                        
                    }
                     res.data.unshift({
                         id: '编号',
                         user_name:'会员',
                         shop_name:'门店',
                         type_text:'类型',
                         is_give:'账户',
                         price:'金额',
                         sn:'订单编号',
                         createtime:'系统时间',
                         
                         });
                     // 2. 如果需要调整顺序，请执行梳理函数
                     var data = excel.filterExportData(res.data, [
                         'id',
                         'user_name',
                         'shop_name',
                         'type_text',
                         'is_give',
                         'price',
                         'sn',
                         'createtime',
                         
                     ]);
                     // 3. 执行导出函数，系统会弹出弹框
                     excel.exportExcel({
                         sheet1: data
                     }, '会员卡记录.xlsx', 'xlsx');
                 }
             });
          })
    });
</script>
